package cn.starremote.component.utils;


import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.util.Optional;

/**
 * @descrion 字符串操作工具类
 * @version V1.0
 * @author: Wangjw
 * @date 2024-1-8 21:51
 */
@Slf4j
public class StringUtil extends StringUtils {

    /**
     * 不为null、空字符串、空白字符
     *
     * @param str 字符串
     * @return true or false
     */
    public static boolean isNotEmptyAndBlank(CharSequence str) {
        return isNotEmpty(str) && isNotBlank(str);
    }

    /**
     * 为null、空字符串或者空白字符
     *
     * @param str 字符串
     * @return true or false
     */
    public static boolean isEmptyAndBlank(CharSequence str) {
        return isEmpty(str) || isBlank(str);
    }

    /**
     * 字符串格式化
     *
     * @param template 模版
     * @param params   参数
     * @return 拼接后字符串
     */
    public static String format(CharSequence template, Object... params) {
        return StrUtil.format(template, params);
    }

    /**
     * 补位
     *
     * @param str       字符串
     * @param minLength 长度
     * @param padStr    补位字符串
     * @return 拼接后字符串
     */
    public static String padPre(CharSequence str, int minLength, CharSequence padStr) {
        return StrUtil.padPre(str, minLength, padStr);
    }


    /**
     * splitDoubleValue
     *
     * @param value "1,2"
     * @return DoubleValueBean
     * @see StringUtil#splitDoubleValue(String, String, String)
     */
    public static DoubleValueBean splitDoubleValue(String value) {
        return splitDoubleValue(value, ",", null);
    }

    /**
     * "," return value1=null, value2=null
     * <p>
     * "1," return value1=1, value2=null
     * <p>
     * ",2" return  value1=null, value2=2
     * <p>
     * "1,2" return value1=1, value2=2
     *
     * @param value        "1,2"
     * @param defaultValue 默认值
     * @return DoubleValueBean
     */
    public static DoubleValueBean splitDoubleValue(String value, String regex, String defaultValue) {
        if (StrUtil.isEmpty(value)) {
            return new DoubleValueBean();
        }
        String[] split = value.split(regex);
        return new DoubleValueBean(split, defaultValue);
    }

    @Data
    @NoArgsConstructor
    public static class DoubleValueBean {

        private String[] values = {};

        private String defaultValue;

        public DoubleValueBean(String[] values, String defaultValue) {
            if (values.length > 2) {
                throw new UnsupportedOperationException("Not applicable");
            }
            this.defaultValue = defaultValue;
            this.values = values;
        }

        public String getValue1() {
            return getByIndex(0);
        }

        public String getValue2() {
            return getByIndex(1);
        }

        public String getByIndex(Integer index) {
            if (index < 0 || index > 1) {
                throw new IndexOutOfBoundsException("Index[0 Or 1]");
            }
            if (ArrayUtil.isEmpty(this.values)) {
                return this.defaultValue;
            }
            return Optional.ofNullable(this.values[index]).filter(StringUtil::isNotBlank).orElse(this.defaultValue);
        }

        @Override
        public String toString() {
            return "value1:" + this.getValue1() + ",value2:" + this.getValue2();
        }

    }

    /**
     * 字符串补零
     * @param s
     * @param b
     * @return
     */
    public static String sequence(String s, int b) {
        String q = "";
        if (null == s) {
            s = "";
        }
        if (b <= 0) {
            return q;
        }
        if (s.length() >= b) {
            q = s.substring(s.length() - b);
        } else {
            int t = b - s.length();
            StringBuilder sb = new StringBuilder(s);
            for (int i = 0; i < t; i++) {
                sb.insert(0, "0");
            }
            q = sb.toString();
        }
        return q;
    }
}
